<template>
  <div>
    <div class="address-top">
      <p @click="back">返回</p>
      <p>{{userInfo.Nick?userInfo.Nick:friendId}}</p>
      <p class="no-content"></p>
    </div>
    <!-- 头像 妮称-->
    <div class="flex top-mes padd15 white">
      <img class="img" :onerror="defaultImg" :src="userInfo.Image">
      <p class="name">{{userInfo.Nick?userInfo.Nick:friendId}}</p>
    </div>
    <div class="flex-between mes-list white">
      <p class="name">用户ID</p>
      <p class="val">{{friendId}}</p>
    </div>
    <div class="flex-between mes-list white">
      <p class="name">性别</p>
      <p class="val">{{userInfo.Gender?userInfo.Gender:'保密'}}</p>
    </div>
    <div class="blank"></div>
    <div v-if="friendId!=loginInfo.identifier&&count == 2" class="flex-between switch-list white">
      <p>加入黑名单</p>
      <mt-switch v-model="isBlack" @change="handleChange"></mt-switch>
    </div>
    <!-- 按钮组 -->
    <div v-if="friendId!=loginInfo.identifier&&count == 2" class="bottom-btn">
      <!-- 好友-->
      <div v-show="isFriend&&!isBlack">
        <mt-button size="large" type="primary" class="btn-send" @click="sendMes()">发消息</mt-button>
        <div class="blank"></div>
        <mt-button size="large" class="del-friend" @click="delFriend()">删除好友</mt-button>
      </div>
      <!-- 黑名单-->
      <!--<div v-show="!isFriend&&!isBlack">
        <mt-button size="large" type="primary" class="btn-send" @click="delFriend()">删除好友</mt-button>
      </div>-->
      <!-- 加人-->
      <div v-show="!isFriend&&!isBlack">
        <mt-button size="large" type="primary" class="btn-send" @click="toAdd()">申请好友</mt-button>
      </div>
    </div>
  </div>
</template>
<style lang="less" rel="stylesheet/less" scoped>
  .font16 {
    font-size: .16rem;
  }
  .fontLimit{
    overflow: hidden;
    text-overflow:ellipsis;
    white-space: nowrap;
    display:block;
  }
  .color4a {
    color: #4A4A4A;
  }
  .address-top{
    p:nth-child(2){
      max-width:1.6rem;
      .fontLimit;
    }
  }
  /* 顶部信息 */
  .top-mes {
    height: .8rem;
    align-items: center;
    .img {
      width: .5rem;
      height: .5rem;
      border-radius: 50%;
      margin-right: .1rem;
    }
    .name {
      .font16;
      .color4a;
      .fontLimit;
    }
  }

  /* 信息列表 */
  .mes-list {
    height: .5rem;
    font-size: .14rem;
    border-top: .005rem solid #E8E8E8;
    .name {
      .color4a;
    }
    .val {
      color: #818181;
    }
  }

  /* 屏蔽开关 */
  .switch-list {
    height: .5rem;
    p {
      .color4a;
      font-size: .14rem;
    }
  }
</style>
<script>
  import Client from '../../assets/js/common';
  import {Toast, MessageBox} from 'mint-ui';
  import {checkStatus} from '../../assets/js/login';
  import Chat from '../../assets/js/chat';
  export default {
    data () {
      return {
        userInfo: {},
        isFriend: false,
        isBlack: false,
        count:0,//等待检查完是否在黑名单或好友列表中
        loginInfo: {},
        friendId: this.$route.query.id,
        defaultImg:Client.defaultImg,
      }
    },
    created () {
      checkStatus((loginInfo) => {
        this.loginInfo = loginInfo;
        Chat.getProfilePortrait([this.friendId]).then(data => {
          this.userInfo = data[0];
          //console.log('data',this.userInfo);
          this.checkFriend();
          this.checkBlack();
        })
      });
    },
    methods: {
      checkBlack(){
        Chat.getBlackList(this.loginInfo.identifier).then(data => {
          if(data&&data.length>0){
            data.forEach((val)=>{
              if(val == this.friendId){
                this.isBlack = true;
              }
            })
          }
          this.count++;
        })
      },
      //检测是否好友
      checkFriend(){
        Chat.getAllFriend(this.loginInfo.identifier).then(data => {
          this.count++;
          if(data == null) return;
          let isFriend = data.InfoItem.find((val,i,arr)=>{
            return val.Info_Account == this.friendId;
          });
          if(isFriend !== undefined){
            this.isFriend = true;
          }
        });
      },
      //删除好友
      deleteFriend() {
        let options = {
          'From_Account': this.loginInfo.identifier,
          'To_Account': [this.friendId],
          //Delete_Type_Both'//单向删除："Delete_Type_Single", 双向删除："Delete_Type_Both".
          'DeleteType': "Delete_Type_Both"
        };
        webim.deleteFriend(
          options,
          resp => {
            Toast('删除好友成功');
          },
          err => {
            Toast('删除好友失败');
            console.log(err.ErrorInfo);
          }
        );
      },
      // 返回
      back(){
        this.loginInfo.onPendencyAddNotify = null;
        history.back();
      },
      //加入黑名单
      handleChange () {
        if (this.isBlack) {
          // 加入黑名单
          let options = {
            'From_Account': this.loginInfo.identifier,
            'To_Account': [this.friendId]
          };
          webim.addBlackList(
            options,
            resp => {
              this.isFriend = false;
              Toast('加入黑名单成功');
              /*setTimeout(function () {
                history.back()
              }, 500)*/
            },
            err => {
              Toast('加入黑名单失败');
              //alert(err.ErrorInfo);
            }
          );
        }else{
          this.cancelBlackFriend();
        }
      },
      // 删除好友确认
      delFriend(val){
        MessageBox.confirm('确定执行此操作?').then(action => {
          this.deleteFriend();
          Toast('删除好友成功');
          this.$router.push('/addressList');
        }, () => {
        });
      },
      // 发信息
      sendMes(){
        this.$router.push({path: '/session', query: {id: this.friendId, selType: 'C2C'}})
      },
      // 申请好友
      toAdd() {
        if(this.friendId == this.loginInfo.identifier){
          Toast("该用户是您本人,无法添加");
          return;
        }
        const allowType = this.userInfo.AllowType;
        //判断对方是否允许加好友
        if (allowType == 2) {
          //需要验证
          this.$router.push({path: 'friendAddSure', query: {userInfo: JSON.stringify(this.userInfo)}})
        } else if (allowType == 3) {
          //拒绝任何人加加好友
          Toast("该用户拒绝任何人添加好友");
          this.$router.push('/addressList');
        } else {
          //允许任何人
          //申请加好友
          this.addFriendApi();
        }
      },
      addFriendApi(){
          let add_friend_item = [
            {
              To_Account: this.userInfo.To_Account,
              AddSource: "AddSource_Type_Unknow",
              AddWording: "" //加好友附言，可为空
            }
          ];
          let options = {
            From_Account: this.loginInfo.identifier,
            AddFriendItem: add_friend_item
          };

          webim.applyAddFriend(
            options,
            resp => {
              if (resp.Fail_Account && resp.Fail_Account.length > 0) {
                for (let i in resp.ResultItem) {
                  console.log(resp.ResultItem[i].ResultInfo);
                  return;
                }
              } else {
                Toast("添加好友成功");
                this.$router.push('/addressList');
              }
            },
            err => {
              if (err.ErrorInfo.indexOf('30525') > -1) {
                Toast("好友请求已发送");
                return;
              }
              if (err.ErrorInfo.indexOf('30520') > -1) {
                Toast('该好友已添加');
                return;
              }
              Toast("添加好友失败");
              console.log('ErrorInfo',err);
            }
          );
      },
      // 移出黑名单
      cancelBlackFriend(){
        let options = {
          'From_Account': this.loginInfo.identifier,
          'To_Account': [this.friendId]
        };
        webim.deleteBlackList(
          options,
          resp => {
            Toast('已将该好友移出黑名单');
            //history.back();
          },
          err => {
            Client.errInfo();
            //alert(err.ErrorInfo);
          }
        );
      }
    }
  }

</script>
